const express = require('express')
const mongoose = require('mongoose')
const bodyParser = require('body-parser')
const cors = require('cors')
//在npm包的这个地址下 https://www.npmjs.com/package/passport
const passport = require('passport')
const app = express()

// 引入users.js
const usersRouter = require('./routers/api/users')
const profileRouter = require('./routers/api/profiles')
// 跨域


// DB config
const db = require('./config/keys').mongoURI
//connect to mongodb
mongoose.connect(db).then(()=>console.log('MongoDB CONECT')).catch((err)=>console.log(err))

// 对passport进行初始化
app.use(passport.initialize());

require('./config/passport')(passport)

// 使用body-parser中间件
app.use(bodyParser.urlencoded({extended:false}))
app.use(bodyParser.json())
app.use(cors())


app.get('/',(req,res)=>{
  res.send('hello world')
})


// 前面呢是默认地址，使用routers
app.use('/api/users',usersRouter)
app.use('/api/profile',profileRouter)

const port = process.env.PORT || 5000


app.listen(port,function(){
   console.log('Server running on port')
})
